<?php
class Topic extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return '{{topic}}';
	}

	public function attributeLabels()
	{
		return array(
			'title'=>'标题',
			'content'=>'内容',
		);
	}

	public function rules()
	{
		return array(
			array('uid,gid,title,content','required'),
		);
	}

	public function relations()
	{
		return array(
			'group'=>array(self::BELONGS_TO,'Group','gid'),
			'author'=>array(self::BELONGS_TO,'User','uid'),
			'comment'=>array(self::HAS_MANY,'Comment','rela_id',
								'condition'=>'comment.rela_type ='.ENTITY_TYPE_TOPIC),
		);
	}

	public function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->created=new CDbExpression('now()');
			}
			$this->content=nl2br($this->content);
		}
		return true;
	}
}
?>
